﻿Shader "Dan/UI/UI_LoadingCircle"
{
    Properties
    {
       _Color ("Color", Color) = (0.07843138,0.3921569,0.7843137,1)
        _Color2 ("Color2", Color) = (1,0,0,1)
        _Speed ("Speed", Float ) = 1
        _Emission ("Emission", Float ) = 1
        _RoadingLevel ("Roading.Level", Float ) = 8
        _Width ("Width", Range(0, 1)) = 0.9
        _Radius ("Radius", Range(0, 1)) = 1
        _Radius_inside ("Radius_inside", Range(0, 1)) = 0.4456896
        _ShinnyAmount ("ShinnyAmount", Float ) = 0.1
        _ShinnySpeed ("ShinnySpeed", Float ) = 1
        [MaterialToggle] _UsePercentage ("UsePercentage", Float ) = 0
        _Loading_Percentage ("Loading_Percentage", Range(0, 1)) = 0
        
    }
    SubShader
    {
        Tags {
            "Queue"="AlphaTest"
            "RenderType"="Transparent"}
        LOD 100

        Pass
        {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            Cull Off
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase_fullshadows
            #pragma only_renderers d3d9 d3d11 glcore gles metal 
            #pragma target 3.0
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
               
                float4 vertex : SV_POSITION;
            };

             float4 _Color;
             float _Speed;
             float _Emission;
             float _ShinnySpeed;
             float _RoadingLevel;
             float _ShinnyAmount;
             float _Loading_Percentage;
             fixed _UsePercentage;
             float _Width;
             float _Radius;
             float _Radius_inside;
             float4 _Color2;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                
                return o;
            }

            

            float Flick (float2 timeSpeed){
               float2 sk = timeSpeed + 0.2127+timeSpeed.x*0.3713*timeSpeed.y;
               float2 rnd = 4.789*sin(489.123*(sk));
               return frac(rnd.x*rnd.y*(1+sk.x));
           }
            
           
           float DrawRing(float shape , float radius , float radiusinside){
            float val =saturate(abs(step(radius,shape)-step(min(radius,radiusinside),shape)));
            return val;
           }
           //间隔旋转效果
           float PolarGap (float polar,float speed ,float usePer, float loadingPer , float width , float level){

               float usePercent = lerp( frac(_Time.y*speed), loadingPer, usePer ); 
               float polarfrac = floor(polar * level) / (level - 1);
               float oneminusPolar = 1.0 - polarfrac;
               float s1 =step(0.5,(-usePercent*2.2+1.65)+oneminusPolar);
               float s2 =step(0.5,(usePercent*2.2-0.55)+polarfrac);
               float gap =1.0 - step(width,frac(polar*level));
               return s1*s2*gap;
           }

            


            fixed4 frag (v2f i) : SV_Target
            {
                float2 uv = i.uv;
                float2 tempUV = uv*2-1;
                //求极坐标
                float polar = atan2(tempUV.x,tempUV.y)/6.28318530718+0.5;
                //求圆
                float circle = length(tempUV);
                //极坐标分隔旋转
                float gap = PolarGap(polar,_Speed,_UsePercentage,_Loading_Percentage,_Width,_RoadingLevel);
                //求圆环
                float ring =DrawRing(circle,_Radius,_Radius_inside);
                //剔除
                clip(gap*ring - 0.5);
                //闪烁时间
                float st = _Time.x*_ShinnySpeed;
                //随机闪烁
                float2 rd = float2(st,st);
                float flick = Flick(rd);
                
                //根据极坐标计算颜色渐变
                float3 emissive = lerp(_Color2.rgb,_Color.rgb,1-polar)*_Emission;
                //加上闪烁
                float3 finalColor = emissive+saturate(flick)*_ShinnyAmount;
                return fixed4(finalColor,1);
                
                
            }
            ENDCG
        }
    }
}
